package com.pzhu.controller.wx;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pzhu.model.entity.Review;
import com.pzhu.model.entity.ReviewImage;
import com.pzhu.model.help.ResponseResult;
import com.pzhu.service.ReviewImageService;
import com.pzhu.service.ReviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author : zqh
 * @Date : 2023/12/27 0027 下午 8:32
 * @Version: 1.0
 */
@Api(tags = "评价相关接口")
@RestController
@RequestMapping("/wx/review")
public class WxReviewController {

    @Autowired
    private ReviewService reviewService;

    @Autowired
    private ReviewImageService reviewImageService;


    @ApiOperation("添加订单信息")
    @GetMapping("/findAll/{userId}")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true)
    public ResponseResult findAll(@PathVariable(value = "userId") Long userId) {
        return reviewService.findAll(userId);
    }

    @ApiOperation("添加或修改评价")
    @PostMapping("/save")
    public ResponseResult save(@RequestBody @Valid Review review){
        if(review.getId()==null || review.getId()==-1){
            review.setCreateTime(new Date());
            reviewService.save(review);

            //保存评价照片
            String[] imgList = review.getImgList();
            List<ReviewImage> reviewImages = new ArrayList<>();
            for (String s : imgList) {
                ReviewImage reviewImage = new ReviewImage();
                reviewImage.setReviewId(review.getId());
                reviewImage.setUrl(s);
                reviewImage.setCreateTime(new Date());
                reviewImages.add(reviewImage);
            }
            reviewImageService.saveBatch(reviewImages);
        }else{
            //修改评价
            review.setUpdateTime(new Date());
            reviewService.updateById(review);
        }
        return ResponseResult.ok();
    }
}
